/*!
 * node-util
 * Copyright(c) 2019 huangxin <3203317@qq.com>
 * MIT Licensed
 */
'use strict';

const hmacSHA1 = require('crypto-js/hmac-sha1');
const Base64 = require('crypto-js/enc-base64');

const _ = require('underscore');

let _exp = module.exports;

const _paramsSort = obj => {
  let _keys = _.keys(obj);
  let _sortBy = _.sortBy(_keys, key => key);

  let _arr = [];

  for (let _key of _sortBy) {
    _arr.push(_key);
    _arr.push('=');
    _arr.push(obj[_key]);
  }

  return _arr;
}

/**
 *
 */
const _genSignature = _exp.genSignature = (data, seckey) => {
  let _arr = _paramsSort(data);
  let _v = _arr.join('');

  let _hash = hmacSHA1(_v, seckey);
  let _sign = Base64.stringify(_hash);

  return _sign;
};
